<?php
/*
 *author:Yangligao
 *Date:2013-9-21
 */

require 'Config.php';

include_once 'Public/Include/adv.php';
include_once 'Public/Include/footer.php';
// 这个flag为了节省一个页面:让注册与注册处理页面在一起
$flag = $_GET['flag'];

if ($flag == 'register'){	
	$smarty->assign('positionFlag','用户注册');
	$smarty->display('Index/register.html');
	
}else if($flag == 'registerAction'){
// 	echo "<pre>";
// 	var_dump($_POST);
// 	echo "</pre>";
	$userModel = new UserModel();
	$res = $userModel->user_select($_POST['user_name']);
	
	if(count($res) == 0){
		$checkPro = $_POST['checkpro'];

		if($checkPro == 'on'){
			if($_POST['user_pwd'] == $_POST['user_repwd']){
				unset($_POST['checkpro']);
				unset($_POST['user_repwd']);
				unset($_POST['x']);
				unset($_POST['y']);
				$arr['user_pwd'] = $_POST['user_pwd'];
				$_POST['user_pwd'] = md5($_POST['user_pwd']);
				
				$resId = $userModel->user_insert($_POST);
				$arr['user_id'] = $resId;
				
				//将明文密码写到user_back备份表中，方便密码找回
				$userBackModel = new UserBackModel();
				$backResId = $userBackModel->user_back_insert($arr);
				if($resId && $backResId){
					$_SESSION['user_name'] = $_POST['user_name'];
					$_SESSION['loginFlag'] = 1;
					jump('恭喜你！<br/>注册成功!<br/>系统已为你自动登录！<br/>', 'index.php');
				}else{
					jump("注册失败！", $_SERVER['HTTP_REFERERE']);
				}
	
			}
		}else{
			jump("协议未同意，注册失败！", $_SERVER['HTTP_REFERERE']);
		}
	}else{
		jump("用户名重复，注册失败！", $_SERVER['HTTP_REFERERE']);
	}
}

?>
